/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/wechat-share/code/model"
)

var Domains = []*mux.Action{
	{
		Method: "GET", Route: "wechat-domains", Name :"Get WeiChat Domains",
		Params: mux.Parameters{
			{
				In: mux.Query,
				Schema: &getWechatDomains{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getWechatDomains{}
			ctx.WarnParse(p)
			ds, err := model.QueryDomain(p.WeiChatID)
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.SendNamedJson("list", ds)
			}
		},
	},
	{
		Method: "POST", Route: "wechat-domain", Name :"Create WeiChat Domain",
		Params: mux.Parameters{
			{
				In: mux.Form,
				Schema: &model.Domain{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Domain{}
			ctx.WarnParse(p)
			err := p.Create()
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.SendNamedJson("id", p.ID)
			}
		},
	},
	{
		Method: "PUT", Route: "wechat-domain", Name :"Update WeiChat Domain",
		Params: mux.Parameters{
			{
				In: mux.Form,
				Schema: &model.Domain{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Domain{}
			ctx.WarnParse(p)
			err := p.Save()
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.MsgSuccess("修改成功", "")
			}
		},
	},
	{
		Method: "DELETE", Route: "wechat-domain", Name :"Update WeiChat Domain",
		Params: mux.Parameters{
			{
				In: mux.Query,
				Schema: &model.Domain{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Domain{}
			ctx.WarnParse(p)
			err := p.Delete()
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.MsgSuccess("删除成功", "")
			}
		},
	},
}

type (
	getWechatDomains struct {
		WeiChatID int `required:"微信配置ID 不能为空"`
	}
)
